package com.example.hangzhou.welbeing.mytestdemoapp.adpater;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * 杭州惟君医疗科技有限公司16
 * Created by 20433 on 2018/4/17
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public abstract class QuickAdapter<T> extends RecyclerView.Adapter<QuickAdapter.VH> {
    private List<T> mData;

    public QuickAdapter(List<T> data) {
        this.mData = data;
    }
    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return VH.get(parent, getLayoutId(viewType));
    }
    @Override
    public void onBindViewHolder(VH holder, int position) { //
        convert(holder, mData.get(position), position);
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }
    protected abstract void convert(VH holder, T date, int position);

    public abstract int getLayoutId(int viewType);

    static class VH extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews;
        private View mConvertView;

        public VH(View itemView) {
            super(itemView);
            mConvertView = itemView;
            mViews = new SparseArray<>();
        }

        public static VH get(ViewGroup parent, int layoutId) {
            View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
            return new VH(convertView);

        }

        public <T extends View> T getView(int id) {
            View view = mViews.get(id);
            if (view == null) {
                view = mConvertView.findViewById(id);
                mViews.put(id, view);
            }
            return (T) view;
        }

        public void setText(int id, String value) {
            TextView view = (TextView) mViews.get(id);
            view.setText(value);

        }
    }
}
